#include "CameraSystem.h"

CCamera::CCamera()
{
	m_vPosition		= D3DXVECTOR3(200.0f, 100.0f, 200.0f);
	m_vRight		= D3DXVECTOR3(1.0f, 0.0f, 0.0f);
	m_vUp			= D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	m_vLook			= D3DXVECTOR3(0.0f, 0.0f, 1.0f);
	m_vOffSet		= D3DXVECTOR3(0.0f,10.0f,-10.0f);
}

CCamera::CCamera(CameraType type)
{
	m_vPosition		= D3DXVECTOR3(20.0f, 20.0f, 20.0f);
	m_vRight		= D3DXVECTOR3(1.0f, 0.0f, 0.0f);
	m_vUp			= D3DXVECTOR3(0.0f, 1.0f, 0.0f);
	m_vLook			= D3DXVECTOR3(0.0f, 0.0f, 1.0f);
	m_vOffSet		= D3DXVECTOR3(0.0f,10.0f,-10.0f);

	Type = type;
}

void CCamera::Fly(float velocity)
{
	if (Type == FREECAMERA)
	{
		m_vPosition.y += velocity * m_vUp.y;
	}

}

void CCamera::Walk(float velocity)
{
	if (Type == FREECAMERA)
	{
		m_vPosition += velocity * m_vLook;
	}
	else
	{
		m_vPosition += velocity * D3DXVECTOR3(m_vLook.x, 0.0f, m_vLook.z);
	}
}

void CCamera::Strafe(float velocity)
{
	if (Type == FREECAMERA)
	{
		m_vPosition += velocity * m_vRight;
	}
	else
	{
		m_vPosition += velocity * D3DXVECTOR3(m_vRight.x, 0.0f, m_vRight.z);
	}
}

void CCamera::Rotate(float x, float y, float z)
{
	D3DXMATRIX	mtxRotate;

	if (x)
	{
		D3DXMatrixRotationAxis(&mtxRotate, &m_vRight, D3DXToRadian(x));
		D3DXVec3TransformNormal(&m_vUp, &m_vUp, &mtxRotate);
		D3DXVec3TransformNormal(&m_vLook, &m_vLook, &mtxRotate);
	}

	if (y)
	{
		if (Type ==  FREECAMERA)
		{
			D3DXMatrixRotationAxis(&mtxRotate, &m_vUp, D3DXToRadian(y));	
		}
		else
		{
			D3DXMatrixRotationY(&mtxRotate, D3DXToRadian(y));
		}
		D3DXVec3TransformNormal(&m_vRight, &m_vRight, &mtxRotate);
		D3DXVec3TransformNormal(&m_vLook, &m_vLook, &mtxRotate);
	}

	if (z && Type == FREECAMERA)
	{
		D3DXMatrixRotationAxis(&mtxRotate, &m_vLook, D3DXToRadian(z));
		D3DXVec3TransformNormal(&m_vUp, &m_vUp, &mtxRotate);
		D3DXVec3TransformNormal(&m_vRight, &m_vRight, &mtxRotate);
	}
}

void CCamera::Release()
{

}

void CCamera::GetPosition(D3DXVECTOR3 *pos)
{
	pos->x = m_vPosition.x;
	pos->y = m_vPosition.y;
	pos->z = m_vPosition.z;
}

void CCamera::SetPosition(D3DXVECTOR3 pos)
{
	m_vPosition.x = pos.x;
	m_vPosition.y = pos.y;
	m_vPosition.z = pos.z;
}

void CCamera::SetLook(D3DXVECTOR3 look)
{
	m_vLook.x = look.x - m_vPosition.x;
	m_vLook.y = look.y - m_vPosition.y;
	m_vLook.z = look.z - m_vPosition.z;
}

void CCamera::GetMatrixView(D3DXMATRIX *V)
{
	D3DXVec3Normalize(&m_vLook, &m_vLook);

	D3DXVec3Cross(&m_vUp, &m_vLook, &m_vRight);
	D3DXVec3Normalize(&m_vUp, &m_vUp);

	D3DXVec3Cross(&m_vRight, &m_vUp, &m_vLook);
	D3DXVec3Normalize(&m_vRight, &m_vRight);

	float posX = - D3DXVec3Dot(&m_vPosition, &m_vRight);
	float posY = - D3DXVec3Dot(&m_vPosition, &m_vUp);
	float posZ = - D3DXVec3Dot(&m_vPosition, &m_vLook);

	V->_11 = m_vRight.x;	V->_12 = m_vUp.x;	V->_13 = m_vLook.x;		V->_14 = 0.0f;
	V->_21 = m_vRight.y;	V->_22 = m_vUp.y;	V->_23 = m_vLook.y;		V->_24 = 0.0f;
	V->_31 = m_vRight.z;	V->_32 = m_vUp.z;	V->_33 = m_vLook.z;		V->_34 = 0.0f;
	V->_41 = posX;			V->_42 = posY;		V->_43 = posZ;			V->_44 = 1.0f;
}